package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbOrderItemExample;
import com.pyg.pojo.TbOrderItemExample.Criteria;
import com.pyg.mapper.TbOrderItemMapper;
import com.pyg.pojo.TbOrderItem;
import com.pyg.goods.service.TbOrderItemService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbOrderItemServiceImpl implements TbOrderItemService {
	@Autowired
	private TbOrderItemMapper tbOrderItemMapper;

	@Override
	public List<TbOrderItem> findAll() {
		return tbOrderItemMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbOrderItem> page = (Page<TbOrderItem>) tbOrderItemMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbOrderItem
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbOrderItem tbOrderItem, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbOrderItemExample example = null;
		if (tbOrderItem != null) {
			example = new TbOrderItemExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbOrderItem.getItemId())) {
				criteria.andItemIdEqualTo(tbOrderItem.getItemId() );
			}
			if (PygStringUtils.isNotEmpty(tbOrderItem.getGoodsId())) {
				criteria.andGoodsIdEqualTo(tbOrderItem.getGoodsId() );
			}
			if (PygStringUtils.isNotEmpty(tbOrderItem.getOrderId())) {
				criteria.andOrderIdEqualTo(tbOrderItem.getOrderId() );
			}
			if (PygStringUtils.isNotEmpty(tbOrderItem.getTitle())) {
				criteria.andTitleLike("%" + tbOrderItem.getTitle() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrderItem.getPrice())) {
				criteria.andPriceEqualTo(tbOrderItem.getPrice() );
			}
			if (PygStringUtils.isNotEmpty(tbOrderItem.getNum())) {
				criteria.andNumEqualTo(tbOrderItem.getNum() );
			}
			if (PygStringUtils.isNotEmpty(tbOrderItem.getTotalFee())) {
				criteria.andTotalFeeEqualTo(tbOrderItem.getTotalFee() );
			}
			if (PygStringUtils.isNotEmpty(tbOrderItem.getPicPath())) {
				criteria.andPicPathLike("%" + tbOrderItem.getPicPath() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbOrderItem.getSellerId())) {
				criteria.andSellerIdLike("%" + tbOrderItem.getSellerId() + "%");
			}
		}

		Page<TbOrderItem> page = (Page<TbOrderItem>) tbOrderItemMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbOrderItem tbOrderItem) {
		tbOrderItemMapper.insert(tbOrderItem);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbOrderItemMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbOrderItem findOne(Long id) {
		return tbOrderItemMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbOrderItem tbOrderItem) {
		tbOrderItemMapper.updateByPrimaryKey(tbOrderItem);
	}

}
